package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.FollowStudent;
import cn.wolfcode.crm.query.FollowStudentQueryObject;
import cn.wolfcode.crm.service.IFollowStudentService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("followStudent")
public class FollowStudentController {

    @Autowired
    private IFollowStudentService followStudentService;

    @RequestMapping("index")
    public String index(){
        return "followStudent/list";
    }

    @ResponseBody
    @RequestMapping("list")
    public PageResult index(FollowStudentQueryObject qo){
        PageResult result = followStudentService.queryListByQo(qo);
        return result;
    }

    @ResponseBody
    @RequestMapping("save")
    public JsonResult save(FollowStudent followStudent){
        JsonResult result = new JsonResult();
        try {
            followStudentService.insert(followStudent);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存出错!");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("update")
    public JsonResult update(FollowStudent followStudent){
        JsonResult result = new JsonResult();
        try {
            followStudentService.updateByPrimaryKey(followStudent);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("编辑出错!");
        }
        return result;
    }

    //跟踪学员审核
    @ResponseBody
    @RequestMapping("audit")
    public JsonResult audit(FollowStudent followStudent){
        JsonResult result = new JsonResult();
        try {
            followStudentService.audit(followStudent);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("审核出错!");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("delete")
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            if (id!=null){
                followStudentService.deleteByPrimaryKey(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("删除出错!");
        }
        return result;
    }

    //根据潜在学员Id查询该学员被跟踪次数
    @ResponseBody
    @RequestMapping("queryFollowNumberByCustomerId")
    public int queryFollowNumberByCustomerId(Long id){
        return followStudentService.queryFollowNumberByCustomerId(id);
    }

    //根据潜在学员Id查询该学员最后一次被跟踪的数据
    @ResponseBody
    @RequestMapping("queryFollowByCustomerId")
    public FollowStudent queryFollowByCustomerId(Long id){
        return followStudentService.queryFollowByCustomerId(id);
    }

}
